#include<bits/stdc++.h>

using namespace std;

class Solution {
public:
    int findMaxLength(vector<int> &nums) {
        unordered_map<int, int> hash;
        int N = nums.size();
        vector<int> oneSum(N, 0);
        vector<int> zeroSum(N, 0);
        oneSum[0] = nums[0];
        zeroSum[0] = nums[0] ^ 1;
        int maxLen = 0;
        for (int i = 1; i < N; ++i) {
            oneSum[i] = oneSum[i - 1] + nums[0];
            zeroSum[i] = zeroSum[i - 1] + (nums[0] ^ 1);
            int diff = oneSum[i] - zeroSum[i];
            if (zeroSum[i] == oneSum[i]) {
                maxLen = i + 1;
                continue;
            }
            if (hash.find(diff) != hash.end())
                maxLen = max(maxLen, i - hash[diff]);
            else
                hash[diff] = i;
        }
        return maxLen;
    }
};

int main() {

}